﻿Imports Tirailleur

Public Class cDemoClient

    Private mCurrentSimulationTime As DateTime

    Public Sub Demo()

        Dim SpreadField As cDummySpreadField
        Dim InitialPath() As IMetricCoordinateTwoDimensional
        Dim Path As FirePerimeterPath
        Dim OneMinute As TimeSpan
        Dim TimeTicker As Integer
        Dim AdvanceToTime As DateTime

        SpreadField = New cDummySpreadField

        'Make a clockwise diamond of four points
        ReDim InitialPath(3)
        InitialPath(0) = New cMetricGridCoordinate(-5, 0)
        InitialPath(1) = New cMetricGridCoordinate(0, 5)
        InitialPath(2) = New cMetricGridCoordinate(5, 0)
        InitialPath(3) = New cMetricGridCoordinate(0, -5)

        mCurrentSimulationTime = #7/7/2012 12:00:00 PM#

        Path = New FirePerimeterPath(mCurrentSimulationTime, SpreadField, InitialPath)

        OneMinute = New TimeSpan(0, 0, 60)

        For TimeTicker = 1 To 10
            AdvanceToTime = mCurrentSimulationTime.Add(OneMinute)
            Call AdvanceSimulationTime(Path, AdvanceToTime)
        Next

        For TimeTicker = 1 To 10
            AdvanceToTime = mCurrentSimulationTime.Subtract(OneMinute)
            Call AdvanceSimulationTime(Path, AdvanceToTime)
        Next

    End Sub

    Private Sub AdvanceSimulationTime(ByVal Path As FirePerimeterPath, ByVal NewTime As DateTime)

        Dim RemainingSecondsToSimulate As Double
        Dim SimulationDirection As eFireModelDirection
        Dim TimeStepLengthSeconds As Double
        Dim MaximumSafeTimeStepSeconds As Double
        Dim CurrentStep As TimeSpan

        RemainingSecondsToSimulate = NewTime.Subtract(mCurrentSimulationTime).TotalSeconds

        If RemainingSecondsToSimulate < 0 Then
            SimulationDirection = eFireModelDirection.efmdBackwards
        Else
            SimulationDirection = eFireModelDirection.efmdForward
        End If

        Do While RemainingSecondsToSimulate <> 0

            TimeStepLengthSeconds = Math.Abs(RemainingSecondsToSimulate)
            MaximumSafeTimeStepSeconds = Path.GetMaximumSafeStepLengthInSeconds

            If TimeStepLengthSeconds < MaximumSafeTimeStepSeconds Then
                CurrentStep = TimeSpan.FromSeconds(TimeStepLengthSeconds)
            Else
                CurrentStep = TimeSpan.FromSeconds(MaximumSafeTimeStepSeconds)
            End If

            Path.AdvancePath(CurrentStep, SimulationDirection)

            If SimulationDirection = eFireModelDirection.efmdForward Then
                mCurrentSimulationTime = mCurrentSimulationTime.Add(CurrentStep)
            Else
                mCurrentSimulationTime = mCurrentSimulationTime.Subtract(CurrentStep)
            End If

            RemainingSecondsToSimulate = NewTime.Subtract(mCurrentSimulationTime).TotalSeconds

        Loop
    End Sub

End Class
